package com.questetra.bpms.air.util
{
    import com.adobe.serialization.json.JSON;
    import com.questetra.bpms.air.model.Workitem;
    
    
	public class WorkitemParser
	{
		public function WorkitemParser()
		{
		}

        public static function JSONObjectToWorkitems(value:Object):Array /* of Workitem */
        {
            try
            {
                var jsonDecodedObject:Object = JSON.decode(String(value));
            }
            catch(e:Error)
            {
                trace("ERROR " + e);
                return null;
            }
            
            var workitems:Array /* of Workitem */ = [];
            
            for each(var value:Object in jsonDecodedObject)
            {
                if (value is Array)
                {
                    for each(var item:Object in value)
                    {
                        var workitem:Workitem = JSONObjectToWorkitem(item);
                        workitems.push(workitem);
                    }
                }
            }

            return workitems;
        }

		private static function JSONObjectToWorkitem(value:Object):Workitem
		{
			var workitem:Workitem = new Workitem();

            workitem.id = value.id;
            workitem.processModelInfoId = value.processModelInfoId;
            workitem.processModelInfoName = value.processModelInfoName;
            workitem.processModelVersion = value.processModelVersion;
            workitem.nodeNumber = value.nodeNumber;
            workitem.nodeName = value.nodeName;
            workitem.swimlaneType = value.swimlaneType;
            workitem.processInstanceId = value.processInstanceId;
            workitem.processInstanceState = value.processInstanceState;
            workitem.processInstanceTitle = value.processInstanceTitle;
//TODO 日付は変換要
//            workitem.processInstanceStartDatetime = value.processInstanceStartDatetime;
//            workitem.processInstanceEndDatetime = value.processInstanceEndDatetime;
            workitem.processInstanceInitQgroupId = value.processInstanceInitQgroupId;
            workitem.processInstanceInitQgroupName = value.processInstanceInitQgroupName;
            workitem.processInstanceInitQuserId = value.processInstanceInitQuserId;
            workitem.processInstanceInitQuserName = value.processInstanceInitQuserName;
            workitem.state = value.state;
//            workitem.startDatetime = value.startDatetime;
//            workitem.offerDatetime = value.offerDatetime;
//            workitem.allocateDatetime = value.allocateDatetime;
//            workitem.endDatetime = value.endDatetime;
//            workitem.timeLimitDatetime = value.timeLimitDatetime;
            workitem.allocatedQgroupId = value.allocatedQgroupId;
            workitem.allocatedQgroupName = value.allocatedQgroupName;
            workitem.allocatedQuserId = value.allocatedQuserId;
            workitem.allocatedQuserName = value.allocatedQuserName;

//			t.activity_node_name = value.activity.node.name;
//			t.activity_node_swimlane_swimlaneType = value.activity.node.swimlane.swimlaneType;
//			t.createDatetime = value.createDatetime;
//			t.startDatetime = value.startDatetime;
//			t.timeLimitDatetime = value.timeLimitDatetime;
//			t.id = value.id;
//			t.processInstance_id = value.processInstance.id;
//			t.processInstance_initQgroupId = value.processInstance.initQgroupId;
//			t.processInstance_initQgroupName = value.processInstance.initQgroupName;
//			t.processInstance_initQuserId = value.processInstance.initQuserId;
//			t.processInstance_initQuserName = value.processInstance.initQuserName;
//			t.processInstance_processModelInfo_name = value.processInstance.processModelInfo.name;
//			t.processInstance_startDatetime = value.processInstance.startDatetime;
//			t.processInstance_title = value.processInstance.title;
			
			return workitem;
		}
		
//		public static function SavebjectToTaskView(value:Object):IQ_LocalTask{
//			var t:Q_LocalTaskBase = new Q_LocalTaskBase();
//			t.activity_node_name = value.activity_node_name;
//			t.activity_node_swimlane_swimlaneType = value.activity_node_swimlane_swimlaneType;
//			t.createDatetime = value.createDatetime;
//			t.startDatetime = value.startDatetime;
//			t.timeLimitDatetime = value.timeLimitDatetime;
//			t.id = value.id;
//			t.processInstance_id = value.processInstance_id;
//			t.processInstance_initQgroupId = value.processInstance_initQgroupId;
//			t.processInstance_initQgroupName = value.processInstance_initQgroupName;
//			t.processInstance_initQuserId = value.processInstance_initQuserId;
//			t.processInstance_initQuserName = value.processInstance_initQuserName;
//			t.processInstance_processModelInfo_name = value.processInstance_processModelInfo_name;
//			t.processInstance_startDatetime = value.processInstance_startDatetime;
//			t.processInstance_title = value.processInstance_title;
//			
//			t.x = value.x;
//			t.y = value.y;
//			t.depth = value.depth;
//			t.color_num = value.color_num;
//			
//			t.local_key = value.local_key;
//			return t;
//		}
	}
}